/*
 * @Description: 
 * @Version: 0.1
 * @Autor: 许振富 zfgj201511
 * @Date: 2025-01-17 10:56:12
 * @FilePath: \hlk_protocol\zw0906_protocol.c
 * @LastEditors: 许振富(jia)
 * @LastEditTime: 2025-01-17 15:01:40
 */
#include "zw0906_protocol.h"
#include "string.h"

unsigned short lrc_clc(unsigned char * _buf,unsigned short _len)
{
    unsigned short l_lrc = 0x07;
    unsigned short each = 0;
    for (each = 0; each < _len; each++)
    {
        l_lrc += _buf[each];
    }
    return l_lrc;
}

unsigned char zw0906_protocol_Data_Packaging(unsigned char _flag,unsigned char _cmd,
                                           unsigned char *_Data, unsigned short _Len,
                                           unsigned char *_Pack_Buff, unsigned short *_Pack_Len)
{
#define PADDING_DATA(x) _Pack_Buff[Count++] = x
    unsigned short LRC = 0; // LRC
    unsigned int Count = 0;
    unsigned short each = 0;

    PADDING_DATA(0xEF);
    PADDING_DATA(0x01);
    PADDING_DATA(0xFF);
    PADDING_DATA(0xFF);
    PADDING_DATA(0xFF);
    PADDING_DATA(0xFF);
    PADDING_DATA(_flag);
    PADDING_DATA(((_Len+3)&0xFF00) >> 8);
    PADDING_DATA(((_Len+3)&0xFF));
    PADDING_DATA(_cmd);
    LRC += _flag;
    LRC += ((_Len+3)&0xFF00) >> 8;
    LRC += (_Len+3)&0xFF;
    LRC += _cmd;
    for ( each = 0; each < _Len; each++)
    {
        PADDING_DATA(_Data[each]);
        LRC += _Data[each];
    }
    PADDING_DATA((LRC&0xFF00) >> 8);
    PADDING_DATA((LRC&0xFF));
    *_Pack_Len = Count;
    return 0;
}

unsigned char zw0906_protocol_Init(__zw0906_protocol* _protocol)
{
	_protocol->count = 0;
	_protocol->Flag = zw0906_Head;
	_protocol->LRC = 0;
	return 0;
}

unsigned char zw0906_protocol_Reset(__zw0906_protocol* _protocol)
{
	_protocol->count = 0;
	_protocol->Flag = zw0906_Head;
	_protocol->LRC = 0;
	return 0;
}
unsigned char zw0906_protocol_Receive_Byte(__zw0906_protocol* Protocol,unsigned char _Data)
{
    switch (Protocol->Flag)
    {
    case zw0906_Head:
        // Protocol->head_buf <<=8;
        // Protocol->head_buf |= _Data;
        Protocol->head_buf[Protocol->count] = _Data;
        Protocol->count += 1;
        if(Protocol->count == 7)
        {
            Protocol->count = 0;
            if(memcmp((unsigned char *)&Protocol->head_buf,"\xef\x01\xff\xff\xff\xff\x07",7) == 0)
            {
                Protocol->Flag = zw0906_LEN;
            }
        }
        
        break;
    case zw0906_LEN:
        Protocol->count += 1;
        Protocol->Rx_Buffer.Length <<= 8;
        Protocol->Rx_Buffer.Length |= _Data;
        if(Protocol->count == 2)
        {
            Protocol->Flag = zw0906_ENTER;
        }
        break;
    case zw0906_ENTER:
        Protocol->Rx_Buffer.enter = _Data;
        Protocol->count = 0;
        if (Protocol->Rx_Buffer.Length == 3)
        {
            Protocol->Flag = zw0906_LRC;
        }
        else
        {
            Protocol->Flag = zw0906_DATA;
        }

        break;
    case zw0906_DATA:
        Protocol->Rx_Buffer.Data[Protocol->count] = _Data;
        Protocol->count += 1;
        if(Protocol->count == (Protocol->Rx_Buffer.Length - 3))
        {
            Protocol->count = 0;
            Protocol->Flag = zw0906_LRC;
        }
        break;
    case zw0906_LRC:
        Protocol->count += 1;
        Protocol->LRC <<= 8;
        Protocol->LRC |= _Data;
        if(Protocol->count == 2)
        {
            if(Protocol->LRC == lrc_clc((unsigned char*)&Protocol->Rx_Buffer.Length,Protocol->Rx_Buffer.Length))
            {
                Protocol->Flag = zw0906_Valid;
            }
        }
        break;
    default:
        break;
    }
	return 0;
}
